package uebung03_w;

import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.util.ArrayList;

import javax.imageio.ImageIO;
import javax.swing.BoxLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;

import tools.ShowImage;

public class ScaleSpace {
	public static void main(String[] args) throws Exception {
		
		ArrayList<BufferedImage> scales = new ArrayList<BufferedImage>();
		ArrayList<AffineTransform> transforms = new ArrayList<AffineTransform>();
		
		final BufferedImage g0 = ImageIO.read(ScaleSpace.class.getClassLoader().getResourceAsStream("res/kermit001.jpg"));
		
		scales.add(g0);
		transforms.add(new AffineTransform());
		
		AffineTransform scaler = AffineTransform.getScaleInstance(0.5, 0.5);
		
		BufferedImage bi = new BufferedImage(g0.getWidth(), g0.getHeight(), BufferedImage.TYPE_INT_ARGB);
		
		int[] rgb = new int[bi.getWidth()*bi.getHeight()];
		
		for (int i = 0; i<4; i++) {
			
			BufferedImage gi = new BufferedImage(bi.getWidth(), bi.getHeight(), BufferedImage.TYPE_INT_ARGB);
			Graphics2D g2 = ((Graphics2D)gi.getGraphics());
			g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
			g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
			
			g2.drawImage(g0, scaler, null);
			
			scales.add(gi);
			transforms.add(new AffineTransform(scaler));
			
			scaler.concatenate(AffineTransform.getScaleInstance(0.5, 0.5));
		}
		
		JFrame ff = new JFrame();
		ff.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		JPanel panel = new JPanel();
		
		panel.setLayout(new BoxLayout(panel, BoxLayout.Y_AXIS));
		
		JScrollPane jsp = new JScrollPane(panel);
		for (int i = 0; i<scales.size(); i++) {
			panel.add(new ShowImage(scales.get(i), transforms.get(i).createInverse()));
		}
		ff.add(jsp); 
		
		ff.pack();
		ff.setVisible(true);
	}
}
